<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>20200722JS作业</title>

    <script>


        //1:计算工资计算所交个税金额
        function tax(a = parseInt(a)) {
            var money = 0;
            if (a > 0 && a <= 3500) {
                money = 0;
            } else if (a > 3500 && a <= 8000) {
                money = (a - 3500) * 0.02;
            } else if (a > 8000 && a <= 20000) {
                money = (a - 8000) * 0.05;
            } else if (a > 20000) {
                money = (a - 200000) * 0.1;
            } else {
                alert("金额输入有误！");
                return
            }
            alert("需要缴税" + money);
        }

        //2：用js实现 百钱买白鸡问题，100元买100只鸡，大公鸡5元一只，老母鸡3元一只，小鸡一元三只，有多少种买法。
        function buyJi() {
            var count = 0;
            for (var i = 0; 5 * i <= 100; i++) {
                for (var j = 0; 3 * j <= 100; j++) {
                    for (var k = 0; 1 * k <= 100; k++) {
                        if (((i + j + k * 3) == 100) && ((5 * i + 3 * j + 1 * k) == 100)) {
                            count++;
                            alert(count + ":买了" + i + "只大公鸡," + j + "只老母鸡," + (k * 3) + "只小鸡。");
                        }
                    }
                }
            }
        }

        //3：输入一个正整数，获得他的最高位，比如123的最高位是1，10000的最高位是1，9的最高位是9
        function getMax(num = parseInt(num)) {
            while (num > 10) {
                num /= 10;
            }
            alert("最高位:" + parseInt(num));
        }

        //4:水仙花问题
        function shuiXianHua() {
            for (var i = 100; i < 1000; i++) {
                var a = parseInt(i % 10);
                var b = parseInt(i / 10 % 10);
                var c = parseInt(i / 100);
                // if (((i % 10) * (i % 10) * (i % 10) + (i / 10 % 10) * (i / 10 % 10) * (i / 10 % 10)
                //     + ((i / 100) * (i / 100) * (i / 100))) == i) {
                //     document.write(i + "是水仙花数<br/>");
                // }
                if ((a * a * a + b * b * b + c * c * c) == i) {
                    alert(i + "是水仙花数");
                }
            }
        }

        //5（预习）： 实现时间的动态显示功能，要求能显示星期几，年月日，小时分钟秒，并能随着时间的变化而变化
        function getTime() {
            var now = new Date();
            var year = now.getFullYear();
            var month = now.getMonth();
            var date = now.getDate();
            var hour = now.getHours();
            var min = now.getMinutes();
            var sec = now.getSeconds();
            var week = now.getDay();
            alert(`现在是${year}年${month + 1}月${date}日 ${hour}时${min}分${sec}秒 星期${week}`);
        }

        //7：熟悉ECMAScript 6的新语法特性，写个例子
        function str1() {
            // 普通字符串
            let str1 = "This is line1 \n This is line2";
            alert(str1);
        }

        function str2() {
            // 多行字符串
            let str2 = `This is line1
         This is line2`;
            alert(str2);
        }

        function str3() {
            // 字符串中嵌入变量
            let name = "Tony", time = "today";
            let str = `Hello ${name}, how are you ${time}?`
            alert(str);
        }


    </script>
</head>
<body>
<input type="button" value="计算个税" onclick="tax(window.prompt('请输入工资:'))">
<input type="button" value="百钱买百鸡" onclick="buyJi()">
<input type="button" value="最高位" onclick="getMax(window.prompt('请输入一个正整数:'))">
<input type="button" value="三位数水仙花" onclick="shuiXianHua()">
<input type="button" value="现在时间" onclick="setInterval(getTime,1000)">
<input type="button" value="普通字符串" onclick="str1()">
<input type="button" value="多行字符串" onclick="str2()">
<input type="button" value="字符串中嵌入变量" onclick="str3()">


</body>
</html>